// TaskStatisticsMapper.java
package com.example.demo.mapper;

import com.example.demo.entity.TaskStatistics;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface TaskStatisticsMapper {
    // 根据课程ID查询所有任务统计
    @Select("SELECT * FROM task_statistics WHERE task_id IN " +
            "(SELECT task_id FROM tasks WHERE course_id = #{courseId})")
    List<TaskStatistics> findByCourseId(@Param("courseId") String courseId);

    // 根据任务ID查询统计
    @Select("SELECT * FROM task_statistics WHERE task_id = #{taskId}")
    TaskStatistics findByTaskId(@Param("taskId") Long taskId);

    // 按查看次数排序查询
    @Select("SELECT ts.* FROM task_statistics ts " +
            "JOIN tasks t ON ts.task_id = t.task_id " +
            "WHERE t.course_id = #{courseId} " +
            "ORDER BY ts.view_count DESC")
    List<TaskStatistics> findByCourseIdOrderByViewCount(@Param("courseId") String courseId);

    // 按讨论次数排序查询
    @Select("SELECT ts.* FROM task_statistics ts " +
            "JOIN tasks t ON ts.task_id = t.task_id " +
            "WHERE t.course_id = #{courseId} " +
            "ORDER BY ts.discussion_count DESC")
    List<TaskStatistics> findByCourseIdOrderByDiscussionCount(@Param("courseId") String courseId);
}